package kafka.demo;

import com.google.common.collect.Lists;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.ByteArrayDeserializer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.junit.Before;
import org.junit.Test;

import java.nio.charset.Charset;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author jiangwz
 * @create 2022/3/9.
 */
public class ConsumerDemo {

    private KafkaConsumer<String, byte[]> consumer;

    @Before
    public void init(){
        System.setProperty("log.root.level", "info");
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 8192);
        props.put(ConsumerConfig.MAX_PARTITION_FETCH_BYTES_CONFIG, 16777216);
        props.put(ConsumerConfig.FETCH_MAX_BYTES_CONFIG, 134217728);
        props.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, 300000);

        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);

        props.put(ConsumerConfig.GROUP_ID_CONFIG, "default");
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");

        consumer = new KafkaConsumer<String, byte[]>(props);
        consumer.subscribe(Lists.newArrayList("test01"));
    }

    @Test
    public void testConsume(){

        while (true) {
            ConsumerRecords<String, byte[]> records = consumer.poll(Duration.ofMillis(1000));
            List<ConsumerRecord<String, byte[]>> recordList = new ArrayList<>(256);
            records.forEach(recordList::add);
            for (ConsumerRecord<String, byte[]> record : recordList) {
                System.out.println(record.headers());
                System.out.println(new String(record.value(), Charset.forName("UTF-8")));
                System.out.println(record.offset());
                System.out.println(record.key());
                System.out.println(record.partition());
                System.out.println(record.topic());
                System.out.println(record.timestamp());

            }
            consumer.commitAsync();

        }
    }
}
